package com.bocai.eelly.activitys;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bocai.eelly.R;
import com.bocai.eelly.adapter.DialogAdapter;
import com.bocai.eelly.bean.BaseBean;
import com.bocai.eelly.common.BaseActivity;
import com.bocai.eelly.common.Loading;
import com.bocai.eelly.common.ToolbarHelper;
import com.bocai.eelly.utils.DialogUtil;
import com.bocai.eelly.utils.L;
import com.bocai.eelly.utils.T;
import com.bocai.eelly.utils.Utils;
import com.orhanobut.dialogplus.DialogPlus;
import com.orhanobut.dialogplus.OnItemClickListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class PerfectInformationActivity extends BaseActivity implements View.OnClickListener {

    @Bind(R.id.tv_title)
    TextView tvTitle;
    @Bind(R.id.toolbar)
    Toolbar toolbar;
    @Bind(R.id.ll_regist_perfect_information_dismiss)
    LinearLayout llRegistPerfectInformationDismiss;
    @Bind(R.id.et_regist_perfect_information_company)
    EditText etRegistPerfectInformationCompany;
    @Bind(R.id.et_regist_perfect_information_addr)
    EditText etRegistPerfectInformationAddr;
    @Bind(R.id.tv_regist_perfect_information_field)
    TextView tvRegistPerfectInformationField;
    @Bind(R.id.et_regist_perfect_information_connect)
    EditText etRegistPerfectInformationConnect;
    @Bind(R.id.et_regist_perfect_information_product)
    EditText etRegistPerfectInformationProduct;
    @Bind(R.id.tv_skip_perfect)
    TextView tvSkipPerfect;
    @Bind(R.id.tv_regist_perfect_information)
    TextView tvRegistPerfectInformation;
    @Bind(R.id.ll_choose_industry)
    LinearLayout llChooseIndustry;
    StringBuffer stringBuffer=new StringBuffer();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_perfect_information);
        ButterKnife.bind(this);
        initView();
        initEvent();
    }

    private void initEvent() {
        llRegistPerfectInformationDismiss.setOnClickListener(this);
        tvRegistPerfectInformation.setOnClickListener(this);
        tvSkipPerfect.setOnClickListener(this);
        llChooseIndustry.setOnClickListener(this);
    }

    private void initView() {
        ToolbarHelper.setup(this, R.string.str_title_perfect_information, R.mipmap.ic_back, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_skip_perfect:
                startActivity(new Intent(this, HomeActivity.class));
                break;
            case R.id.ll_regist_perfect_information_dismiss:
                llRegistPerfectInformationDismiss.setVisibility(View.GONE);
                break;
            case R.id.ll_choose_industry://选择所属行业
                String [] title={"服装制造","贸易公司","面料","辅料","货运代理","设备制造"};
                List<String> titles=new ArrayList<>();
                for (int i=0;i<title.length;i++)
                {
                    titles.add(title[i]);
                }
                DialogAdapter adapter=new DialogAdapter(this,titles);
                DialogUtil.showDialog(this, adapter, new OnItemClickListener() {
                    @Override
                    public void onItemClick(DialogPlus dialog, Object item, View view, int position) {
                        stringBuffer.append(item.toString());
                        tvRegistPerfectInformationField.setText(stringBuffer);
                        stringBuffer.append(",");
                        dialog.dismiss();
                    }
                });
                break;
            case R.id.tv_regist_perfect_information:
                Loading.show(this,getString(R.string.str_loading));
                String timeline = Utils.getTimeline();
                String sign = Utils.md5(timeline);
                L.d(stringBuffer.toString());
                getTestApi().registPerfectInformation(timeline, sign, Utils.getUserId(this, "id"), etRegistPerfectInformationCompany.getText().toString(),
                        etRegistPerfectInformationAddr.getText().toString(), tvRegistPerfectInformationField.getText().toString(),
                        etRegistPerfectInformationConnect.getText().toString(), etRegistPerfectInformationProduct.getText().toString())
                        .compose(this.<BaseBean>bindToLifecycle())
                        .subscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribe(new Subscriber<BaseBean>() {
                            @Override
                            public void onCompleted() {

                            }

                            @Override
                            public void onError(Throwable e) {
                                Loading.dismiss();
                                L.d(e.toString());
                                T.showShort(PerfectInformationActivity.this, R.string.net_error);
                            }

                            @Override
                            public void onNext(BaseBean baseBean) {
                                Loading.dismiss();
                                T.showShort(PerfectInformationActivity.this, baseBean.getMsg());
                            }
                        });
                break;
        }
    }
}
